import re

import pandas as pd
import numpy as np


# 清理不是年份的数据
def clean(filename, name):
    pattern1 = re.compile(r'[\u4e00-\u9fff]')
    # 打开csv文件
    file = pd.read_csv(filename)
    file1 = ~file['publish_time'].str.contains(pattern1)
    file2 = file[file1]
    file2.to_csv(name + '_clean.csv', index=None)


# 获取年份
def getYear(filename, name):
    file = pd.read_csv(filename)
    file1 = np.array(file)
    x = []
    for row in file1:
        x.append(row[7].split('-')[0])
    year = pd.DataFrame({'年份': x})
    year.to_csv(name + '_year.csv', index=None)


# 统计年份出现的次数
def Count(filename, name):
    df = pd.read_csv(filename)
    # 统计次数
    counts = df['年份'].value_counts()
    result = pd.DataFrame({'年份': counts.index, '次数': counts.values})
    result.to_csv('../../../Data/zhangjinyang/talk/' + name + '_counts.csv', index=None)


if __name__ == '__main__':
    clean('../../../Data/zhangjinyang/books_prose.csv', 'prose')
    clean('../../../Data/zhangjinyang/books_literature.csv', 'literature')
    clean('../../../Data/zhangjinyang/books_novel.csv', 'novel')
    clean('../../../Data/zhangjinyang/books_poetry.csv', 'poetry')
    getYear('literature_clean.csv', 'literature')
    getYear('novel_clean.csv', 'novel')
    getYear('prose_clean.csv', 'prose')
    getYear('poetry_clean.csv', 'poetry')
    Count('literature_year.csv', 'literature')
    Count('novel_year.csv', 'novel')
    Count('poetry_year.csv', 'poetry')
    Count('prose_year.csv', 'prose')
